import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/automounting_provider.dart';
import 'package:xiongan_app_yy/provider/autopicking_provider.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//自动拣选——详情列表
class AutoPickingDetailList extends StatefulWidget {
  String id;
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AutoPickingDetailList();
  }

  AutoPickingDetailList(this.id);
}

//ListWidget
class _AutoPickingDetailList extends State<AutoPickingDetailList> {
  List<dynamic> _data = List();

  @override
  Widget build(BuildContext context) {
    _data = Provider.of<AutoPickingProvider>(context).lowerShelfList;
    return Scaffold(
        appBar: AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "自动拣选详情列表",
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body: LoadBuilder(
          visibility: Provider.of<AutoPickingProvider>(context).netStatus2,
          builder: Column(
            children: <Widget>[
              Expanded(
                child: RefreshIndicator(child:  ListView.separated(
                    itemBuilder: (BuildContext context, int index) {
                      return TextCartItem(
                        data: _data,
                        index: index,
                        onTapFuns: (int index) {
                          Provider.of<AutoPickingProvider>(context)
                              .setDetailData(obj: _data[index]);
                          Navigator.of(context).pushNamed("/autopicking_detail",
                              arguments: widget.id).then((state){
                            if(state==true){
                              Provider.of<AutoPickingProvider>(context)
                                  .getDetailListData(orderid:widget.id);
                            }
                          });
                        },
                      );
                    },
                    separatorBuilder: (BuildContext context, int index) {
                      return Divider(color: Colors.black26);
                    },
                    itemCount: _data.length), onRefresh:_refresh)

              ),
             /* Row(
                children: <Widget>[
                  Expanded(
                      child: FlatButton(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(0))),
                    padding: EdgeInsets.all(15),
                    color: Color(0xFF42AFF0),
                    child: Text(
                      "提交",
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: () {
                      int number = 0;
                      for (int i = 0; i < _data.length; i++) {
                        if (_data[i]["ispickup"] == true) {
                          number++;
                        }
                      }
                      if (number == _data.length) {
                        Provider.of<AutoPickingProvider>(context)
                            .SubmissionAutoPickingDetail(sendObj: {
                          "id": widget.id,
                          'operaterUserId': DataSingleton().Data.id,
                          'tenantId': DataSingleton().Data.tenantId,
                          'Warehouseid': DataSingleton().warehouseChoice.id,
                        }).then((a) {
                          Provider.of<AutoPickingProvider>(context).getList();
                          Navigator.pop(context);
                        });
                      } else {
                        TostUtil.showToast("请拣选完所有物资种类");
                      }
                    },
                  ))
                ],
              )*/
            ],
          ),
        ));
  }

    Future <void> _refresh()  async{
      await Future.delayed(Duration(seconds: 1),(){
          return null;
      });
       Provider.of<AutoPickingProvider>(context)
        .getDetailListData(orderid:widget.id);
  }
}

class _Text extends Text {
  TextStyle style;
  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}

class TextCartItem extends StatelessWidget {
  final List<dynamic> data;
  final int index;
   Function(int index) onTapFuns;

  TextCartItem(
      {Key key, @required this.data, @required this.index, this.onTapFuns})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Color textColor =
        data[index]['ispickup'] == true ? Color(0xFF2FC480) : Color(0xFF00A5F1);
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              //解决GestureDetector Widget事件触发面问题
              border: Border.all(color: Colors.transparent, width: 1)),
          child: Column(
            children: <Widget>[
              Align(
                alignment: Alignment.bottomLeft,
                child: _Text("物资名称:${data[index]['materialname']}"),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("下架库区:${data[index]['areaname']}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("下架货架:${data[index]['shelfname']??""}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("下架货位:${data[index]['fromlocationcode']}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: RichText(
                    text: TextSpan(
                      text: '拣选状态:',
                      style:
                          TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                      children: <TextSpan>[
                        TextSpan(
                          text: data[index]['ispickup'] == true ? "已拣选" : "未拣选",
                          style: TextStyle(fontSize: 14.0, color: textColor),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          )),
      onTap: () {
        if (onTapFuns != null) {
          onTapFuns(index);
        }
      },
    );
  }
}
